﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;

namespace Rdz.Security
{
	public class RdzCryptography
	{
		///////////////////////////////////////Cryptography////////////////////////////////////////////

		/// <summary>
		/// Encrypt a string using dual encryption method. Returns a encrypted text with no Hashing.
		/// </summary>
		/// <param name="TexttoEncrypt">String to be encrypted</param>
		/// <param name="SecurityKey">Security Key string</param>
		/// <returns>Returns encrypted string.</returns>
		public static string Encrypt(string TexttoEncrypt, string SecurityKey)
		{
			return Encrypt(TexttoEncrypt, SecurityKey, false);
		}
		/// <summary>
		/// Encrypt a string using dual encryption method. Returns a encrypted text.
		/// </summary>
		/// <param name="TexttoEncrypt">String to be encrypted</param>
		/// <param name="SecurityKey">Security Key string</param>
		/// <param name="useHashing">true if use hashing for extra security</param>
		/// <returns>Returns encrypted string.</returns>
		public static string Encrypt(string TexttoEncrypt, string SecurityKey, bool useHashing)
		{
			try
			{
				byte[] keyArray;
				byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(TexttoEncrypt);

				if (useHashing)
				{
					MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
					keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecurityKey));
					hashmd5.Clear();
				}
				else
					keyArray = UTF8Encoding.UTF8.GetBytes(SecurityKey);


				RC2CryptoServiceProvider tdes = new RC2CryptoServiceProvider();
				tdes.Key = keyArray;
				tdes.Mode = CipherMode.ECB;
				tdes.Padding = PaddingMode.PKCS7;

				ICryptoTransform cTransform = tdes.CreateEncryptor();
				byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
				tdes.Clear();
				return Convert.ToBase64String(resultArray, 0, resultArray.Length);
			} catch (Exception)
			{
				throw;
			}
		}


		/// <summary>
		/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string with no Hashing.
		/// </summary>
		/// <param name="TexttoDecrypt">String to be decrypted</param>
		/// <param name="SecurityKey">Security Key string</param>
		/// <returns>Returns decrypted text.</returns>
		public static string Decrypt(string TexttoDecrypt, string SecurityKey)
		{
			return Decrypt(TexttoDecrypt, SecurityKey, false);
		}
		/// <summary>
		/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string.
		/// </summary>
		/// <param name="TexttoDecrypt">String to be decrypted</param>
		/// <param name="SecurityKey">Security Key string</param>
		/// <param name="useHashing">true if use hashing for extra security</param>
		/// <returns>Returns decrypted text.</returns>
		public static string Decrypt(string TexttoDecrypt,string SecurityKey, bool useHashing)
		{
			try
			{
				byte[] keyArray;
				byte[] toEncryptArray = Convert.FromBase64String(TexttoDecrypt);

				//System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
				////Get your key from config file to open the lock!
				//string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

				if (useHashing)
				{
					MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
					keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecurityKey));
					hashmd5.Clear();
				}
				else
					keyArray = UTF8Encoding.UTF8.GetBytes(SecurityKey);

				RC2CryptoServiceProvider tdes = new RC2CryptoServiceProvider();
				tdes.Key = keyArray;
				tdes.Mode = CipherMode.ECB;
				tdes.Padding = PaddingMode.PKCS7;

				ICryptoTransform cTransform = tdes.CreateDecryptor();
				byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

				tdes.Clear();
				return UTF8Encoding.UTF8.GetString(resultArray);
			} catch (Exception)
			{
				throw;
			}
		}

		///////////////////////////////////////Cryptography////////////////////////////////////////////
	}
}
